﻿using System;
using System.Text;
using Microsoft.SharePoint;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Alcero.SharePoint.Data
{
    class Utility
    {
        /// <summary>
        /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
        /// </summary>
        /// <param name="characters">Unicode Byte Array to be converted to String</param>
        /// <returns>String converted from Unicode Byte Array</returns>
        public static String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            return (constructedString);
        }

        /// <summary>
        /// Deserializes a base 64 encoded string using a binary formatter.
        /// </summary>
        /// <param name="data">Data to deserialize</param>
        /// <returns>Object</returns>
        public static object BinaryDeserializeObject(string data)
        {
            if (String.IsNullOrEmpty(data))
            {
                return null;
            }

            IFormatter formatter = new BinaryFormatter();

            try
            {
                using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(data)))
                {
                    return (object)formatter.Deserialize(stream);
                }
            }
            catch (Exception ex)
            {
                //Log
            }
            return null;
        }

        /// <summary>
        /// Converts the String to UTF8 Byte array and is used in De serialization
        /// </summary>
        /// <param name="pXmlString"></param>
        /// <returns></returns>
        public static Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        } 

        /// <summary>
        /// Serialize an object using a binary formatter and returns a base64 encoded string of the serialization
        /// </summary>
        /// <param name="data">The object to serialize</param>
        /// <returns>String</returns>
        public static string BinarySerializeObject(Object data)
        {
            try
            {
                IFormatter formatter = new BinaryFormatter();
                byte[] serializedBytes;

                using (MemoryStream stream = new MemoryStream())
                {
                    formatter.Serialize(stream, data);
                    serializedBytes = stream.ToArray();
                }
                return Convert.ToBase64String(serializedBytes);
            }

            catch (Exception ex)
            {
                //Log
            }

            return null;
        }


        /// <summary>
        /// This function retrieve an SPWeb in elevated priviledge using a string parameter.
        /// </summary>
        /// <param name="web">A Spweb the specify the current web context.</param>
        /// <returns>SPWeb</returns>
        public static SPWeb GetElevatedWeb(string web)
        {
            SPWeb elevatedRootWeb = null;

            if (!String.IsNullOrEmpty(web))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(web))
                    {
                        using (SPWeb tempWeb = site.OpenWeb())
                        {
                            elevatedRootWeb = tempWeb;
                        }
                    }

                });
            }

            return elevatedRootWeb;
        }

        /// <summary>
        /// This function retrieve an SPWeb in elevated priviledge.
        /// </summary>
        /// <param name="web">A Spweb the specify the current web context.</param>
        /// <returns></returns>
        public static SPWeb GetElevatedWeb(SPWeb web)
        {
            SPWeb elevatedRootWeb = null;

            //Return the contextual web object
            if (null == web)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                    {
                        using (SPWeb tempWeb = site.OpenWeb())
                        {
                            elevatedRootWeb = tempWeb;
                        }
                    }

                });
            }
            else
            {

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(web.Url))
                    {
                        using (SPWeb tempWeb = site.OpenWeb())
                        {
                            elevatedRootWeb = tempWeb;
                        }
                    }

                });
            }

            return elevatedRootWeb;
        }
    }
}
